package light.head.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.cache.Cache;
import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AnnouncementDAO;
import light.head.dao.sys.MemorandumDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.OperationLogDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.OrganTypeHandleDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.dto.NewMessage;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.SystemInfo;
import light.head.model.Announcement;
import light.head.model.Memorandum;
import light.head.model.OperationLog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;


/**
 * @author janhang
 * @2011-8-17  下午12:00:22
 * 用户管理
 */
@Controller
@RequestMapping(value = Url.C_MAIN)
public class MainController extends SupporterController {
	
	
	private OrganTypeHandleDAO othDAO;
	private UserInfoDAO userDAO;
	private MemorandumDAO memoDAO;
	private OperationLogDAO operDAO;
	private MessageDAO messageDAO;
	private OrganDAO organDAO;
	private AnnouncementDAO annDAO;

	
	@RequestMapping(value = Sys.NONE)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cu_email = cu_email(request);
		Integer cu_organCode = cu_organCode(request);
		Boolean isManager = cu_isManager(request);
		/*Organ rootOrgan = organDAO.getRoot(cu_organCode);
		Integer rootCode = null != rootOrgan ? rootOrgan.getCode() : null;*/
		
		if ((null == cu_roleCode(request) || null == cu_email) && !Sys.ADMIN.equals(cu_email) && (null == cu_isManager(request) || !cu_isManager(request))) {
			return new ModelAndView(Url.INDEX);
		}
		
		/********** 获取菜单 **********/
		List<SystemInfo> systemInfoList = Cache.systemInfoList;	// 系统的所有菜单
		Map<String, Boolean> orgMap = new HashMap<String, Boolean>();
		Map<String, Boolean> handleMap = new HashMap<String, Boolean>();
		// 判断是否是超级管理员
		if (!Sys.ADMIN.equals(cu_email)) {
			
			// 判断是否是根机构管理员,只有根机构管理员才能不需要角色， && cu_organCode.intValue() != rootCode.intValue()
			if ((null == isManager || !isManager)) {
				// 不是根机构管理员就需要获取角色的权限
				handleMap = getCurrentUserHandleMap(request);
				request.setAttribute(Parm.HANDLE_MAP, handleMap);
			}
			
			Integer ss = organDAO.getRoot(cu_organCode).getOtc();			// 获取根机构的所有权限
			orgMap = othDAO.getHandleMapByOrganTypeCode(ss);
			request.setAttribute(Parm.ORG_HANDLE_MAP, orgMap);
		}
		
		Map<String, Boolean> tempMap = new HashMap<String, Boolean>();	
		if (null == handleMap || handleMap.size() == 0)
			tempMap = orgMap;
		else
			tempMap = handleMap;
		
		boolean isAdmin = false;
		if (Sys.ADMIN.equals(cu_email))
			isAdmin = true;
		else
			isAdmin = false;
			
		List<SystemInfo> list = new ArrayList<SystemInfo>();
		List<SystemInfo> sysList = new ArrayList<SystemInfo>();
		SystemInfo temp = null;
		
		for (SystemInfo s : systemInfoList) {
			if (isAdmin) {
				list.add(s);
				temp = new SystemInfo();
				temp.setCode(s.getCode());
				temp.setName(s.getName());
				temp.setDes(s.getDes());
				temp.setHide(s.getHide());
				temp.setIcon(s.getIcon());
				temp.setLog(s.getLog());
				temp.setModuleList(null); // 这样做只是不想传更多的数据返回到页面, 下同
				sysList.add(temp);
				continue;
			}
			
			if (null != s.getCode() && null != tempMap.get(s.getCode())) {
				// 判断用户配置的默认显示的系统菜单,
				list.add(s);
				temp = new SystemInfo();
				temp.setCode(s.getCode());
				temp.setName(s.getName());
				temp.setDes(s.getDes());
				temp.setHide(s.getHide());
				temp.setIcon(s.getIcon());
				temp.setLog(s.getLog());
				temp.setModuleList(null);
				sysList.add(temp);
			}
		}
		
		// 显示的菜单
		if (null != list && list.size() > 2 && !isAdmin)
			request.setAttribute(Parm.LIST, list.subList(0, 2));
		else
			request.setAttribute(Parm.LIST, list);	
		
		// 系统选择
		request.setAttribute("sysList", sysList);

		/****************************/
		
		/********** 当前机构的用户信息 **********/
		String userListJson = userDAO.userList(cu_organCode);
		request.setAttribute(Parm.USER_LIST, userListJson);
		
		return new ModelAndView(Url.MAIN);
	}
	
	@RequestMapping(Url.SHOWMEMO)
	public void showMemo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<Memorandum> memoList = memoDAO.findByOcAndCu(cu_organCode(request),cu_code(request));
		if(memoList != null){
			Gson gson = new Gson();
			String json = gson.toJson(memoList);
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(json);
		}
	}

	@RequestMapping(Url.LOAD_DEFAULT)
	public ModelAndView loadDefault(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<OperationLog> list = operDAO.findNew(cu_organCode(request), 8);
		if (cu_organCode(request) != null) {
			Integer oc= organDAO.getRoot(cu_organCode(request)).getCode();
			List<Announcement> list1 = annDAO.findOc(organDAO.getSonCodeList(oc), 10);
			request.setAttribute(Parm.LIST_1, list1);
		}
		request.setAttribute(Parm.LIST, list);
		return new ModelAndView(Url.R_DEFAULT);
	}
	
	@RequestMapping(Url.EDITMEMO)
	public ModelAndView editMemo(String start, String end, Integer code, HttpServletRequest request, HttpServletResponse response){
		Memorandum mem = memoDAO.get(code);
		request.setAttribute(Parm.START, start);
		request.setAttribute(Parm.END, end);
		request.setAttribute(Parm.MEMORANDUM, mem);
		return new ModelAndView(Url.MEMO_EDIT);
	}
	
	@RequestMapping(Url.NEWEST)
	public void newset(Integer code, HttpServletRequest request, HttpServletResponse response){
		List<NewMessage> list = messageDAO.findEmailList(cu_code(request));
		Gson gson = new Gson();
		String json = gson.toJson(list);
		if (isNotNull(json))
			print(response, json);
		else
			print(response, Sys.B);
	}
	
	@RequestMapping(Url.ADDMEMO)
	public void addMemo(Memorandum memo, HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		if(memo.getCode() == null){//添加
			memo.setOc(cu_organCode(request));
			memo.setCu(cu_code(request));
			memo.setDesignee(cu_code(request));
			memo.setTransactor(cu_code(request));
			result = memoDAO.add(memo);
		}else{//修改
			result = memoDAO.update(memo);
		}
		if(result == RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
		
	}
	
	@RequestMapping(Url.DELMEMO)
	public void delMemo(HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			result = memoDAO.del(Integer.parseInt(code));
		}
		
		if(RESULT.SUCCESS == result){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.LOAD_MEMU)
	public ModelAndView loadMemu(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter(Parm.ID);
		if (!isNotNull(id)) {
			// 没有任何权限，
			return new ModelAndView(Url.C_MEMU);
		}
		
		/********** 获取菜单 **********/
		Map<String, Boolean> handleMap = getCurrentUserHandleMap(request);
		request.setAttribute(Parm.HANDLE_MAP, handleMap);
//		List<Module> menu = Cache.moduleList;
		List<SystemInfo> systemInfoList = Cache.systemInfoList;
		// 找出选择的是那个系统
		List<SystemInfo> list = new ArrayList<SystemInfo>();	// 系统的所有菜单
		for (SystemInfo s : systemInfoList) {
			if (null != s.getCode() && s.getCode().equals(Parm.SYS_BASIC)) {
				list.add(s);	// 基础系统菜单
			}
			if (null != s.getCode() && s.getCode().equals(id)) {
				list.add(s);	// 要切换的系统菜单
			}
		}
		
		Integer ss = (Integer) request.getSession().getAttribute(Parm.CU_ORGAN_TYPE_CODE);
		Map<String, Boolean> orgMap = othDAO.getHandleMapByOrganTypeCode(ss);
		request.setAttribute(Parm.LIST, list);
		request.setAttribute(Parm.ORG_HANDLE_MAP, orgMap);
		
		return new ModelAndView(Url.C_MEMU);
	}
	
	@Autowired
	public void setOthDAO(OrganTypeHandleDAO othDAO) {
		this.othDAO = othDAO;
	}
	@Autowired
	public void setUserDAO(UserInfoDAO userDAO) {
		this.userDAO = userDAO;
	}
	@Autowired
	public void setMemoDAO(MemorandumDAO memoDAO) {
		this.memoDAO = memoDAO;
	}

	@Autowired
	public void setOperDAO(OperationLogDAO operDAO) {
		this.operDAO = operDAO;
	}
	@Autowired
	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setAnnDAO(AnnouncementDAO annDAO) {
		this.annDAO = annDAO;
	}
	
	
	

}
